package conson.tech.manage.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import conson.tech.core.constant.CommonConstants;
import conson.tech.manage.api.app.constant.AppCommon;
import conson.tech.manage.api.app.dto.AppConfigDTO;
import conson.tech.manage.app.entity.pojo.AppConfig;
import conson.tech.manage.app.entity.pojo.AppMaintainConfig;
import conson.tech.manage.app.entity.vo.*;
import conson.tech.manage.app.mapper.AppConfigMapper;
import conson.tech.manage.app.mapper.AppMaintainConfigMapper;
import conson.tech.manage.app.service.AppConfigService;
import conson.tech.manage.common.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: AppConfigServiceImpl
 * Description:
 * Author: HXL
 * Date: 2020/4/23 2:59 下午
 * Version: 1.0
 */
@Service("appConfigService")
public class AppConfigServiceImpl implements AppConfigService {

    @Autowired
    private AppConfigMapper appConfigMapper;

    @Autowired
    private AppMaintainConfigMapper appMaintainConfigMapper;
    @Transactional
    @Override
    public int setDefaultApp(DefaultAppRequestVo vo) {
        AppConfig appConfig = new AppConfig();
        appConfig.setId(vo.getId());
        appConfig.setSort(vo.getSort());
        appConfig.setDefaultIndex(vo.getDefaultIndex());
        appConfig.setUpdatedTime(LocalDateTime.now());
        return appConfigMapper.updateById(appConfig);
    }

    @Override
    public Page<AppConfigAllVo> queryAppConfigPage(PageVo vo) {
        Page<AppConfigAllVo> page = new Page();
        page.setCurrent(vo.getPageIndex());
        page.setSize(vo.getPageSize());
       return appConfigMapper.queryAppConfigPage(page);
    }
    @Transactional
    @Override
    public int insertAppConfigByList(List<AppConfig> appConfigList) {
        int result = 0;
        if (!CollectionUtils.isEmpty(appConfigList)){
            for (AppConfig appConfig : appConfigList) {
                if (appConfig != null && appConfig.getNavType() == 1 && !StringUtils.isEmpty(appConfig.getAppId())){
                    //去查询ID页
                    AppMaintainConfig appMaintainConfig = appMaintainConfigMapper.selectOne(Wrappers.<AppMaintainConfig>query().lambda().eq(AppMaintainConfig::getAppId, appConfig.getAppId()));

                    //设置参数
                    appConfig = this.buildAppConfig(appConfig, appMaintainConfig);
                }
                appConfig.setCreatedTime(LocalDateTime.now());
                appConfig.setUpdatedTime(LocalDateTime.now());
                result += appConfigMapper.insert(appConfig);
            }
        }
        return result;
    }
    @Transactional
    @Override
    public int updateAppConfigByList(List<AppConfig> appConfigList) {

        int result = 0;
        if (!CollectionUtils.isEmpty(appConfigList)){
            for (AppConfig appConfig : appConfigList) {
                if (appConfig != null && appConfig.getNavType() == 1 && !StringUtils.isEmpty(appConfig.getAppId())){
                    //去查询ID页
                    AppMaintainConfig appMaintainConfig = appMaintainConfigMapper.selectOne(Wrappers.<AppMaintainConfig>query().lambda().eq(AppMaintainConfig::getAppId, appConfig.getAppId()));

                    //设置参数
                    appConfig = this.buildAppConfig(appConfig, appMaintainConfig);
                }
                appConfig.setUpdatedTime(LocalDateTime.now());
                result += appConfigMapper.updateById(appConfig);
            }
        }
        return result;
    }
    @Transactional
    @Override
    public int deleteAppConfig(Long id) {
        AppConfig appConfig = new AppConfig();
        appConfig.setDelFlag(0);
        appConfig.setId(id);
        appConfig.setUpdatedTime(LocalDateTime.now());
        return appConfigMapper.updateById(appConfig);
    }

    @Override
    public Page<LetterBeanAppVo> queryLetterBeanApp(PageVo pageVo) {
        Page<LetterBeanAppVo> page = new Page();
        page.setCurrent(pageVo.getPageIndex());
        page.setSize(pageVo.getPageSize());
        return appConfigMapper.queryLetterBeanApp(page);
    }

    @Override
    public Integer selectAppNumByGroupId(Long id) {
        return appConfigMapper.selectAppNumByGroupId(id);
    }
    @Transactional
    @Override
    public int deleteAppConfigByGroupId(Long groupId) {
        return appConfigMapper.deleteAppConfigByGroupId(groupId);
    }

    @Override
    public Page<DefaultAppVo> queryDefaultApp(PageVo vo) {
        Page<DefaultAppVo> page = new Page();
        page.setCurrent(vo.getPageIndex());
        page.setSize(vo.getPageSize());
        return appConfigMapper.queryDefaultApp(page);
    }
    @Override
    public List<AppConfigDTO> queryAppConfigList(AppConfig appConfig,Integer versionValue) {

        List<AppConfig> list =   appConfigMapper.queryAppConfigList(appConfig,versionValue);
        return EntityUtils.listToList(list, AppConfigDTO.class);
    }

    @Override
    public List<SelectAppVo> querySelectApp() {
        AppConfig appConfig = new AppConfig();
        appConfig.setDelFlag(AppCommon.APP_DEL_FLAG_ONE);
        appConfig.setDefaultIndex(AppCommon.APP_DEFAULT_ZERO);
        appConfig.setPlateType(AppCommon.APP_PLATE_TYPE_TWO);
        appConfig.setOpen(AppCommon.APP_OPEN_ONE);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(appConfig);
        List<AppConfig> list = appConfigMapper.selectList(queryWrapper);
        List<SelectAppVo> voList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (AppConfig config : list) {
                SelectAppVo vo = new SelectAppVo();
                vo.setId(config.getId());
                vo.setAppName(config.getName());
                voList.add(vo);
            }
        }
        return voList;
    }


    @Override
    public List<AppMaintainConfigVo> getAllAppMaintainConfig() {
        List<AppMaintainConfig> dataList = appMaintainConfigMapper.selectList(Wrappers.<AppMaintainConfig>query().lambda().
                eq(AppMaintainConfig::getDelFlag, CommonConstants.DEL_FLAG_NORMAL));

        if(CollectionUtils.isEmpty(dataList)){
            return null;
        }

        List<AppMaintainConfigVo> resList = new ArrayList<>(dataList.size());

        dataList.stream().forEach(data -> resList.add(this.buildAppMaintainConfigVo(data)));

        return resList;
    }

    @Override
    public List<AppConfig> selectList(QueryWrapper queryWrapper) {
        return appConfigMapper.selectList(queryWrapper);
    }

    @Override
    public List<AppConfigDTO> queryIndexByUserId(Long userId, Integer versionValue) {
        List<AppConfig> list = appConfigMapper.queryIndexByUserId(userId, versionValue);
        return  EntityUtils.listToList(list, AppConfigDTO.class);
    }


    private AppMaintainConfigVo buildAppMaintainConfigVo (AppMaintainConfig appMaintainConfig){
        AppMaintainConfigVo appMaintainConfigVo = new AppMaintainConfigVo();
        if (appMaintainConfig != null) {
            appMaintainConfigVo.setAppId(appMaintainConfig.getAppId());
            appMaintainConfigVo.setAppName(appMaintainConfig.getAppName());
        }
        return appMaintainConfigVo;
    }

    private AppConfig buildAppConfig(AppConfig appConfig, AppMaintainConfig appMaintainConfig){
        if (appMaintainConfig != null) {
           // appConfig.setName(appMaintainConfig.getAppName());
            appConfig.setAppId(appMaintainConfig.getAppId());
            appConfig.setIsTitle(appMaintainConfig.getIsTitle());
            appConfig.setAppType(appMaintainConfig.getAppType());
            appConfig.setIosGoPage(appMaintainConfig.getIosGoPage());
            appConfig.setAndroidGoPage(appMaintainConfig.getAndroidGoPage());
            appConfig.setAppStartUrl(appMaintainConfig.getAppStartUrl());
            appConfig.setGroupInfoId(appMaintainConfig.getGroupInfoId());

        }
        return appConfig;
    }
}
